package com.xr.wage.commonTools;

import com.xr.common.CommUtil;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * bonusTaxPlanViewModel
 */
public class bonusTaxPlanViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
     Dataset ds=getDataset("datasetTaxPlan");
    super.doUpdateData(parameters, outParameters);
    String sql="update WA_BONUS_TAX_PLAN set bonus_tax=pck_common.getBonusTax(bonus,3,2),bonus_get=pck_common.getBonusTax(bonus,3,1) where bonus>0 ";
   CommUtil.executeUpdate(sql);
     sql=" update WA_BONUS_TAX_PLAN set suggest_bonus=pck_common.getBonusTaxBack(bonus_get,3,1),suggest_bonus_tax=pck_common.getBonusTaxBack(bonus_get,3,2) where bonus>0";
   CommUtil.executeUpdate(sql); 
   //sql=" update WA_BONUS_TAX_PLAN set  suggest_bonus_tax=pck_common.getBonusTax(suggest_bonus,3,2) where bonus>0";
   //CommUtil.executeUpdate(sql);
   
   sql="update WA_BONUS_TAX_PLAN t set bonus_rank=(select count(distinct btp.bonus)+1 from WA_BONUS_TAX_PLAN btp where btp.bonus<t.bonus),bonus_get_rank=(select count(distinct btp.bonus_get)+1 from WA_BONUS_TAX_PLAN btp where btp.bonus_get<t.bonus_get) where bonus>0 and bonus_get>0 ";
   CommUtil.executeUpdate(sql); 
   sql="update WA_BONUS_TAX_PLAN t set bonus_rank=0,bonus_get_rank=0 where nvl(bonus,0) =0 and nvl(bonus_get,0)=0 ";
   CommUtil.executeUpdate(sql); 
   sql="update WA_BONUS_TAX_PLAN btp set tax_cha =nvl(btp.bonus_tax,0)+nvl(btp.SEND_MONTH_WAGE_TAX,0)-nvl(btp.S_WAGE_TAX,0)-btp.suggest_bonus_tax where btp.bonus_tax>0 ";
   CommUtil.executeUpdate(sql); 
  }

  public void computer(ParameterSet parameters, ParameterSet outParameters)
  throws Exception { 
	     super.doUpdateData(parameters, outParameters);
	     DBStatement dbs =new DBStatement();
	     Dataset ds=getDataset("datasetTaxPlan");
	     ds.moveFirst();
	     while(!ds.isLast()){
	    	 
	    	 dbs.setSql("begin pck_common.hlbsBonus("+ds.getString("BONUS_PLAN_ID")+"); end;");
	    	 dbs.execute();
	    	 ds.moveNext();
	     }
	     dbs.close();
  }
  public void getWage(ParameterSet parameters, ParameterSet outParameters)
  throws Exception { 
	     super.doUpdateData(parameters, outParameters);
	     DBStatement dbs =new DBStatement();
	     Dataset ds=getDataset("datasetTaxPlan");
	     double wage=0;
	     ds.moveFirst();
	     while(!ds.isLast()){
	    	 if(ds.getString("SEND_MONTH").length()!=6){
	    		 ds.moveNext();
	    	 }
	    	 dbs.setSql("select sum(d.value) from wa_send s,wa_psn p,wa_data d where  s.send_id=p.send_id and p.psn_id=d.psn_id and d.hritems_id=1010 " +
	    	 		"and s.send_month='"+ds.getString("SEND_MONTH")+"' and p.employee_id='"+ds.getString("EMPLOYEE_ID")+"' ");
	    	 wage=dbs.query().getDouble(0);
	    	 if(wage>0){
	    		 dbs.setSql("update WA_BONUS_PLAN set IF_GET_WAGE=1,GET_WAGE_DATE=sysdate,SEND_MONTH_WAGE="+wage+" where BONUS_PLAN_ID="+ds.getString("BONUS_TAX_PLAN_ID"));
	    		 dbs.execute();
	    	 }
	    	ds.moveNext();
	     }
	     dbs.execute();
  }
  public void dr(ParameterSet parameters, ParameterSet outParameters)
  throws Exception { 
	    DBStatement dbs =new DBStatement();
	     Dataset ds=getDataset("datasetTaxPlan");
	     ds.moveFirst();
	     while(!ds.isLast()){
	    	 
	    	 dbs.setSql("delete from WA_BONUS_PLAN where BONUS_PLAN_ID='"+ds.getString("BONUS_PLAN_ID")+"'");
	    	 dbs.execute();
	    	 ds.moveNext();
	     }
	     dbs.close();
  } 
  
}
